new Vue({
    el:'#main',
    data:()=>({
        isshow_search:false,
        search:'',
        montharr:[],
        item:0,//产品or配件
        year:'',//年
        month:'',//月
        listdata:[],
        batch:[],
        batchpro_id:[],
        allche:[],
        allback:[],
        page:1,
        pageSize:10,
        lang_month:[],
        en_month:['This month','January','February','March','April','May','June','July','August','September','October','November','December'],
        zh_month:['本月','1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
        pt_month:["Mês", "de janeiro", "de fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"],
        auth_ids:$('#adminauth_ids').text(),
        admin_ids:$('#admin_ids').val()
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            this.batchpro_id=[]
            this.batch = []
            this.allche = []
            // $('.loding').show()
            if(this.search==""){
                this.isshow_search=false
            }
            $.ajax({
                url:'/admin/buy/plan',
                type:'post',
                data:{
                   page:this.page,
                   limit:this.pageSize,
                   item:this.item,
                   year:this.year,
                   month:this.month,
                   searchText:this.search,
                },
                success:(data)=>{
                    data = JSON.parse(data)
                    $('#allche').prop('checked',false)
                    this.listdata=data.data.plan
                    this.listdata.forEach(element => {
                        this.allche.push(element.id)
                        this.allback.push(element.prochar_id)
                    });
                    if(data.data.size){
                        this.Resetnav(this.page-0,data.data.size)
                    }
                    $('.loding').hide()
                }
            })
        },
        changeitem(item){
            this.batchpro_id=[]
            this.batch=[]
            this.page=1
            this.item=item
            this.getdata()
        },
        changemonth(year,month){
            this.batchpro_id=[]
            this.batch=[]
            this.page=1
            this.month=month,
            this.year=year
            this.getdata()
        },
        allsel(event){
            this.batch = []
            if($(event.target).prop('checked')){
                $(event.target).parents('table').find('input').prop('checked',true)
                this.batch=this.allche
                this.batchpro_id=this.allback
            }else{
                $(event.target).parents('table').find('input').prop('checked',false)
                this.batch=[]
                this.batchpro_id=[]
            }
        },
        onesel(did,pro_id){
            if(this.batch.indexOf(did)==-1){
                this.batch.push(did)
                this.batchpro_id.push(pro_id)
            }else{
                this.batch.splice(this.batch.indexOf(did),1)
                this.batchpro_id.splice(this.batchpro_id.indexOf(pro_id),1)
            }
            var isall=true
            $('td input').each(function() {
                if(!$(this).prop('checked')){
                    isall=false
                }
            })
            $('th input').prop('checked',isall)
        },
        delfun(id){
            if((typeof id=='object')&&id.constructor==Array){
                var data={ids:id}
            }else{
                var data={ids:[id]}
            }
            $.ajax({
                url:'/admin/buy/buy_planDel',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    change_mes_type('.alert',data.status)
                    if(data.status==1){
                        this.getdata()
                    }
                    this.$refs.alert.style.display='block'
                    this.$refs.info.innerHTML=data.msg
                }
            }) 
        },
        //关闭搜索内容
        shdow(){
            this.isshow_search=false
            this.search=""
            this.getdata()
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        banckfun(){
            if(this.isRepeat(this.batchpro_id)){
                change_mes_type('.alert',0)
                this.$refs.alert.style.display='block'
                this.$refs.info.innerHTML="有相同产品的采购计划,不能生成同一个采购合同"
            }else
            $.ajax({
                url:'/admin/buyapi/hasterms',
                type:"post",
                success:data=>{
                    change_mes_type('.alert',data.status)
                    if(this.batch.length && data.status==1){
                        window.localStorage.setItem('month',this.month)
                        window.localStorage.setItem('year',this.year)
                        location.href='/admin/buy/plan2po?plan_ids='+this.batch.join(',')
                    }
                    else{
                        change_mes_type('.alert',2)
                        this.$refs.alert.style.display='block'
                        this.$refs.info.innerHTML=this.$refs.waner.value
                    }
                }
            })
        },
        render(){
            this.isshow_search=true;
            this.page=1;
            this.getdata()
            // this.search=""
        },
        setStorage(){
            var storage=window.localStorage;
            storage.setItem("buy_page", this.page);
            storage.setItem('buy_searchText',this.search);
        },
        isRepeat(arr){
            var hash = {};
            for(var i in arr) {
            if(hash[arr[i]]) //hash 哈希
            return true;
            hash[arr[i]] = true;
            }
            return false;
        }
    },
    mounted(){
        if($('#formMenu').text() != 1){
            if(window.localStorage.getItem('buy_page')){  
                this.page=window.localStorage.getItem('buy_page')
                this.search=window.localStorage.getItem('buy_searchText')
            }
        }
        window.localStorage.removeItem('buy_page')
        window.localStorage.removeItem('buy_searchText')

        if(this.search != '') this.isshow_search = true

        if(window.localStorage.getItem("msg")){
            change_mes_type('.alert',1)
            this.$refs.alert.style.display='block'
            this.$refs.info.innerHTML=window.localStorage.getItem("msg")
            window.localStorage.removeItem("msg")
        }
        if(lang=='en-us'){
            this.lang_month=this.en_month
        }else if(lang=='pt-br'){
            this.lang_month=this.pt_month
        }
        else{
            this.lang_month=this.zh_month
        }
        var year=new Date().getFullYear()
        var current=new Date().getMonth()+1
        this.year=year
        this.month= $('#month_').val() ? $('#month_').val() : current
        this.montharr.push({year:year,month:current})
        for(var i=1;i<=6;i++){
            current++
            if(current>12){
                year++
                current=1
            }
            this.montharr.push({year:year,month:current})
        }
        if(window.localStorage.getItem('year')){
            this.year=window.localStorage.getItem('year')
            this.month=window.localStorage.getItem('month')
            window.localStorage.removeItem('year')
            window.localStorage.removeItem('month')
        }
        this.getdata()

    }
})